package com.zjuh.goodboy.util;


public class NumberUtil {
	
	public static double findFirstDouble(String source, double defaultValue) {
		if (StringUtil.isEmpty(source)) {
			return defaultValue;
		}
		StringBuilder sb = new StringBuilder();
		boolean findDigit = false;
		for (int pos = 0, len = source.length(); pos < len; ++pos) {
			char c = source.charAt(pos);
			if (Character.isDigit(c) || c == '.') {
				findDigit = true;
				sb.append(c);
			} else {
				if (findDigit) {
					break;
				}
			}
		}
		String str = sb.toString();
		if (str.isEmpty()) {
			return defaultValue;
		}
		try {
			return Double.parseDouble(str);
		} catch (Exception e) {
			return defaultValue;
		}
	}
	
	public static float findFirstFloat(String source, float defaultValue) {
		double v = findFirstDouble(source, defaultValue);
		return (float)v;
	}
	public static int findFirstInt(String source, int defaultValue) {
		double v = findFirstDouble(source, defaultValue);
		return (int)v;
	}

}
